from jsonrpc import dispatcher

from common import RpcResponse, RpcStatus
from common.exception import ServiceException, CheckedException
from config import configuration
from service_file import MinioService
from service_rpc.api.basic_api import BasicMethod
from service_sms import DingtalkService


class RegisterMethod(BasicMethod):
    """
    注册rpc方法
    """

    @staticmethod
    @dispatcher.add_method
    def upload_file_to_ding(file_name, file_content, file_type="image", context=None):
        """
        上传媒体文件到钉钉（请关闭VPN代理）
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        # 文件类型修正
        ft = BasicMethod.determine_file_type(file_name)
        if len(ft) > 0:
            file_type = ft
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            media_id = msg_api.upload_file(context, file_name, file_content, file_type)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response(RpcStatus.SUCCESS_CODE, "ok", media_id)

    @staticmethod
    @dispatcher.add_method
    def upload_images(bucket_name, object_name, file_content):
        """
        图像上传
        :param bucket_name: bucket名称
        :param object_name: bucket中的对象名称
        :param file_content: base64编码后的文件
        """
        if not configuration.ENABLE_MINIO:
            return RpcResponse.failure("Minio的文件服务未启用")
        try:
            minio_api = MinioService.MinioApiService()
            minio_api.upload_images(bucket_name, object_name, file_content)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "图片上传成功")

    @staticmethod
    @dispatcher.add_method
    def upload_file(bucket_name, object_name, file_content):
        """
        文件上传
        :param bucket_name: bucket名称
        :param object_name: bucket中的对象名称
        :param file_content: base64编码后的文件
        """
        if not configuration.ENABLE_MINIO:
            return RpcResponse.failure("Minio的文件服务未启用")
        try:
            minio_api = MinioService.MinioApiService()
            minio_api.upload_file(bucket_name, object_name, file_content)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "文件上传成功")

    @staticmethod
    @dispatcher.add_method
    def download(bucket_name, object_name):
        """
        文件下载
        :param bucket_name: bucket名称
        :param object_name: bucket中的对象名称
        :return: base64编码文件
        """
        if not configuration.ENABLE_MINIO:
            return RpcResponse.failure("Minio的文件服务未启用")
        try:
            minio_api = MinioService.MinioApiService()
            encoded_data = minio_api.download_file(bucket_name, object_name)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.success(encoded_data)
